package com.etc.controller;


import com.etc.pojo.vege.Veget;
import com.etc.service.VegetServier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("vege")
public class VegetController {
    @Autowired
    private VegetServier vegetServier;

    @RequestMapping("findbyall")
    public String findByAll(HttpServletRequest request){
        List<Veget> byAll = vegetServier.findByAll();
        request.setAttribute("vegelist",byAll);
        return "findbyall";
    }
    @RequestMapping(value = "deleteById")
    public void deleteById(PrintWriter out,String vid){
        Integer integer = vegetServier.deleteById(Integer.parseInt(vid));
        if(integer == 1){
            out.print(1);
        } else{
            out.print(2);
        }
    }
    @RequestMapping(value = "vegeinsert",method = RequestMethod.POST)
    public void insert(PrintWriter out, @RequestParam(value = "vname") String vname,
                                        @RequestParam(value = "vprice") double vprice,
                                        @RequestParam(value = "vpicture") String vpicture){
        Integer integer = null ;
        if(vname != null || "".equals(vname)) {
            if(vprice != 0 ||"".equals(vprice)) {
                if(vpicture != null || "".equals(vpicture)) {

                        integer = vegetServier.insertById(new Veget(vname, vprice, vpicture));

                }else {
                    out.print(5);
                }
            }else{
                out.print(4);
            }
        }else{
            out.print(3);
        }
        if (integer == 1){
            out.print(1);

        }else {
            out.print(2);
        }

    }
    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String add(){
        return "vegeinsert";
    }
    @RequestMapping(value = "shucai",method = RequestMethod.GET)
    public String addd(){
        return "shucai";
    }
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String adadd(HttpServletRequest request){
        List<Veget> byAll = vegetServier.findByAll();
        request.setAttribute("vegelist",byAll);
        return "shucailist";
    }
}
